/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.WebServiceStub;

import com.sun.imageio.plugins.jpeg.JPEGImageReader;
import com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.FileImageInputStream;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

/**
 *
 * @author mmezhen
 */
@WebService(serviceName = "WebServiceStub")
public class WebServiceStub {

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getArrayOfString")
    public java.lang.String[] getArrayOfString() {
        //TODO write your implementation code here:
        java.lang.String[] str = new java.lang.String[2];
        str[0]="Hello ";
        str[1]="World!";
        return str;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "authorization")
    public int autorization(@WebParam(name = "name") String name, @WebParam(name = "password") String password) {
        //TODO write your implementation code here:
        return 0;
        //if ((name==null) || (password==null)) return -1;
        //if (name.matches("admin")&&(password.matches("admin"))) return 0;
        //else return -1;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "helloWorld")
    public String helloWorld() {
        //TODO write your implementation code here:
        return "HelloWorld!";
    }
    
    public static int[]getImage(String fileName) throws IOException {
    ImageReaderSpi spi = new JPEGImageReaderSpi();
    ImageReader r = new JPEGImageReader(spi);
    r.setInput(new FileImageInputStream(new File(fileName)));

    ImageReadParam param = new ImageReadParam();
    BufferedImage img = r.read(0, param);
    Color[][] result = new Color[img.getHeight()][img.getWidth()];
    for (int i = 0; i < result.length; i++) {
      for (int j = 0; j < result[i].length; j++) {
          result[i][j] = new Color(img.getRGB(j, i));
      }
    }

    int[] res=new int[img.getHeight()*img.getWidth()];

          for (int i = 0; i<res.length-2; i=i+3) {
              int k= (i/3) / img.getWidth();
              int j= (i/3) % img.getWidth();
              res[i]= result[k][j].getRed();  
              res[i+1]= result[k][j].getGreen();  
              res[i+2]= result[k][j].getBlue();  
          } 
          
          
  return res;
 }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getPic")
    public int[] getPic() throws IOException  {
         ImageReaderSpi spi = new JPEGImageReaderSpi();
    ImageReader r = new JPEGImageReader(spi);
    r.setInput(new FileImageInputStream(new File("c:\\pic.jpg")));

    ImageReadParam param = new ImageReadParam();
    BufferedImage img = r.read(0, param);
    Color[][] result = new Color[img.getHeight()][img.getWidth()];
    for (int i = 0; i < result.length; i++) {
      for (int j = 0; j < result[i].length; j++) {
          result[i][j] = new Color(img.getRGB(j, i));
      }
    }

    int[] res=new int[img.getHeight()*img.getWidth()+2];
      res[0]=img.getHeight();
      res[1]=img.getWidth();

          for (int i = 0; i<res.length-2; i=i+3) {
              int k= (i/3) / img.getWidth();
              int j= (i/3) % img.getWidth();
              res[i]= result[k][j].getRed();  
              res[i+1]= result[k][j].getGreen();  
              res[i+2]= result[k][j].getBlue();  
          } 
          
          
  return res;
    }
}
